﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MakeItWeb.Framework;
using System.Web.UI.HtmlControls;

namespace AllNations.UI
{
    public partial class Announcements : MakeItWebPage, IDynamicPage
    {
        public override string SelectedSection
        {
            get
            {
                if (SectionCd == "Main")
                    return "Fellowship";
                else if (SectionCd == "YouthGroup")
                    return "Education";

                return string.Empty;
            }
        }

        public string SectionCd
        {
            get
            {
                return MakeItWebPage.GetQueryString("PageCd") ?? "Main";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            btSaveAnnouncement.Click += new EventHandler(btSaveAnnouncement_Click);
            if (!IsPostBack)
            {
                ResetAnnouncementForm();
            }
            ClearAdminFunctions();
            RegisterAdminFunction("Add new announcement", "javascript:AddAnnouncement(); void(0);", "~/Images/btn-plus.gif");
        }

        void btSaveAnnouncement_Click(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                return;

            StoredProcArgs args = new StoredProcArgs();
            args["pHtmlPageId"] = CurrentHtmlPage.HtmlPageId;
            args["pAnnouncementId"] = new Guid(hidAnnouncementId.Value);
            if (!string.IsNullOrEmpty(txtAnnouncementTitle.Text.Trim()))
                args["pAnnouncementTitleTxt"] = txtAnnouncementTitle.Text.Trim();
            args["pAnnouncementTxt"] = txtAnnouncement.Text.Trim();
            if (RadDateTimePickerFrom.SelectedDate.HasValue)
                args["pEffectiveFrom"] = RadDateTimePickerFrom.SelectedDate.Value;
            if (RadDateTimePickerTo.SelectedDate.HasValue)
                args["pEffectiveTo"] = RadDateTimePickerTo.SelectedDate.Value;
            args["pUserName"] = User.Identity.Name;
            args["pSectionCd"] = SectionCd;
            GenericCud.ExecuteCud("proc_cm_SaveAnnouncement", args);

            ResetAnnouncementForm();
        }

        private void ResetAnnouncementForm()
        {
            hidAnnouncementId.Value = Guid.NewGuid().ToString();
            txtAnnouncementTitle.Text = string.Empty;
            txtAnnouncement.Text = string.Empty;
            RadDateTimePickerFrom.SelectedDate = DateTime.Now;
            RadDateTimePickerTo.SelectedDate = null;
        }

        protected string FormatAnnouncement(string sAnnouncement)
        {
            return sAnnouncement.Replace(Environment.NewLine, string.Format("{0}{1}", "<br/>", Environment.NewLine));
        }

        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pHtmlPageId"] = CurrentHtmlPage.HtmlPageId;
            args["pSectionCd"] = SectionCd;
            args["pAnnouncementId"] = new Guid((sender as LinkButton).CommandArgument);
            GenericCud.ExecuteCud("proc_cm_DeleteAnnouncement", args);
        }

        protected void AnnouncementRow_PreRender(object sender, EventArgs e)
        {
            // this is used to highlight the announcements scheduled to appear in the future
            if (IsPageAdministrator())
            {
                var row = sender as HtmlTableRow;
                if (row != null)
                {
                    var displayDate = DateTime.Parse(row.Cells[1].InnerText);
                    if (displayDate > DateTime.Now)
                        row.Style["background-color"] = "#ffcc66";
                }
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            StoredProcArgs args = new StoredProcArgs();
            args["pHtmlPageId"] = CurrentHtmlPage.HtmlPageId;
            args["pSectionCd"] = SectionCd;
            args["pShowPending"] = IsPageAdministrator();
            rptAnnouncement.DataSource = GenericQry.GetDataSet("proc_cm_ListAnnouncement", args);
            rptAnnouncement.DataBind();

            switch (SectionCd)
            {
                case "Main":
                    LeftNavigationViews.SelectedIndex = viewMain.Index;
                    mainImage.ImageUrl = "~/Images/worship_main.png";
                    break;
                case "YouthGroup":
                    LeftNavigationViews.SelectedIndex = viewYouthGroup.Index;
                    mainImage.ImageUrl = "~/Images/YouthGroup.jpg";
                    Image7.ImageUrl = "~/Images/titleYouthGroupHome.png";
                    break;
            }
        }

        string IDynamicPage.GetPageConfigurationControl()
        {
            return string.Empty;
        }

        string IDynamicPage.PageTypeName
        {
            get { return "Announcements"; }
        }

        protected void lblAnnouncement_PreRender(object sender, EventArgs e)
        {
            (sender as Label).Text = SectionCd == "YouthGroup" ? "Announcement" : "제목";
        }

        protected void lblAnnouncementDate_PreRender(object sender, EventArgs e)
        {
            (sender as Label).Text = SectionCd == "YouthGroup" ? "Announcement Date" : "날짜";
        }

        protected void lblAdminMenu_PreRender(object sender, EventArgs e)
        {
            (sender as Label).Text = SectionCd == "YouthGroup" ? "Admin" : "관리";
        }
    }
}